const path = require('path')
const utils = require('../utils') 
module.exports = async (ctx, next) => {
    const url = ctx.request.url
    let fileUrl = url.replace('/api', '')
    fileUrl = '../data' + fileUrl + '.json'
    fileUrl = path.join(__dirname, fileUrl)
    console.log(fileUrl); 
    try {
        const ret = await utils.getFileJsonData(fileUrl)
        ctx.response.body = {
            code: 0,
            data: JSON.parse(ret),
            message:'请求成功'
        }
    } catch (error) {
        const errorMsg = {
            message: '数据不存在',
            code: 400 
        }
        ctx.response.body = errorMsg
    }
    await next()
}
